package com.hulizuofang.wol.chat.server;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

import org.nicocube.appengine.util.dao.Dao;

import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.gwtplatform.dispatch.server.guice.HandlerModule;
import com.hulizuofang.wol.chat.client.action.LoginAction;
import com.hulizuofang.wol.chat.client.action.SendChatAction;
import com.hulizuofang.wol.chat.server.channel.ChannelProvider;
import com.hulizuofang.wol.chat.server.handler.LoginActionHandler;
import com.hulizuofang.wol.chat.server.handler.SendChatActionHandler;

public class ServerModule extends HandlerModule {
	@Override
	protected void configureHandlers() {
		bind(ChatUserRepository.class);
		try {
			bind(Dao.class).toConstructor(Dao.class.getConstructor(PersistenceManagerFactory.class)).in(Singleton.class);
		} catch (SecurityException e) {
			throw new IllegalStateException("",e);
		} catch (NoSuchMethodException e) {
			throw new IllegalStateException("",e);
		}
		bind(PersistenceManagerFactory.class).toInstance(JDOHelper.getPersistenceManagerFactory("transactions-optional"));
		bind(ChannelService.class).toProvider(new Provider<ChannelService>() {
			@Override
			public ChannelService get() {
				return ChannelServiceFactory.getChannelService();
			}
		}).in(Singleton.class);
		bind(ChannelProvider.class).in(Singleton.class);
		bindHandler(LoginAction.class, LoginActionHandler.class);
		bindHandler(SendChatAction.class, SendChatActionHandler.class);
	}
}
